// utils/date.js
export function formatDate(date, format = 'yyyy-MM-dd HH:mm:ss') {
  if (!date) return ''
  
  date = new Date(date)
  if (isNaN(date.getTime())) return ''

  const padZero = (num) => num.toString().padStart(2, '0')

  const map = {
    'yyyy': date.getFullYear(),
    'MM': padZero(date.getMonth() + 1),
    'dd': padZero(date.getDate()),
    'HH': padZero(date.getHours()),
    'mm': padZero(date.getMinutes()),
    'ss': padZero(date.getSeconds())
  }

  return format.replace(/yyyy|MM|dd|HH|mm|ss/g, matched => map[matched])
}

// 使用示例
// formatDate(new Date(), 'yyyy-MM-dd') → "2023-05-20"
// formatDate(1684567890000, 'HH:mm:ss') → "14:30:45"
